package com.yylending.remotevideo.anychat;

import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatObjectDefine;
import com.bairuitech.anychat.AnyChatObjectEvent;
import com.yyfax.common.CLog;
import com.yylending.remotevideo.AnyChat;
import com.yylending.remotevideo.AnyChatPresenter;
import com.yylending.remotevideo.Global;

/**
 * Created by HenryZhang on 2017/4/22.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class ObjectEvent implements AnyChatObjectEvent {
	private static final String TAG = "ObjectEvent";

	private AnyChatPresenter.View mView;

	/**
	 * 营业厅对象ID
	 */
	private int mBusinessHall;
	/**
	 * 队列ID
	 */
	public int queueId;

	public ObjectEvent(AnyChatPresenter.View view) {
		mView = view;
	}

	public void release() {
		mView = null;
	}

	/**业务对象回调事件，调用AnyChatCoreSDk.ObjectControl方法触发这个回调*/
	@Override
	public void OnAnyChatObjectEvent(int dwObjectType, int dwObjectId, int dwEventType, int dwParam1, int dwParam2, int dwParam3, int dwParam4, String strParam) {
		CLog.i(TAG, "OnAnyChatObjectEvent： dwEventType = " + dwEventType + ", dwObjectType = " + dwObjectType);
		switch (dwEventType) {
			//营业厅数据同步，回调一次返回一个营业厅对象id（有多少营业厅回调多少次）
			case AnyChatObjectDefine.ANYCHAT_OBJECT_EVENT_UPDATE:
				CLog.i(TAG, "数据同步的回调，营业厅or队列：dwObjectId = " + dwObjectId);
				switch (dwObjectType) {//分别进入营业厅、队列触发
					case AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA:
						mBusinessHall = dwObjectId;//营业厅对象id
						String name = AnyChatCoreSDK.ObjectGetStringValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA, mBusinessHall, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_NAME);
						CLog.i(TAG, "准备进入营业厅" + name + "（文档说进入营业厅、队列都会触发）");
						AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA, mBusinessHall, AnyChatObjectDefine.ANYCHAT_AREA_CTRL_USERENTER, -1, 0, 0, 0, "");
						break;
				}
				break;
			//服务区域状态变化
			case AnyChatObjectDefine.ANYCHAT_AREA_EVENT_STATUSCHANGE:
				getRoomUserNumber();
				break;
			//进入营业厅结果回调
			case AnyChatObjectDefine.ANYCHAT_AREA_EVENT_ENTERRESULT:
				CLog.i(TAG, "营业厅ID：" + dwObjectId + "， 进入结果：" + dwParam1);
				if (dwParam1 == 0) {
					getRoomUserNumber();
					mView.loginResult(dwParam1);//为的是开始排队
				} else {
					mView.loginResult(AnyChat.EVENT_CONNECT_BUSINESS_HALL_FAILED);
				}
				break;
			//进入队列结果回调
			case AnyChatObjectDefine.ANYCHAT_QUEUE_EVENT_ENTERRESULT:
				CLog.i(TAG, "进入队列结果：" + dwParam1);
				getRoomUserNumber();
				break;
			//用户队列状态变化事件回调
			case AnyChatObjectDefine.ANYCHAT_QUEUE_EVENT_STATUSCHANGE:
				CLog.i(TAG, "队列数据变化事件（不一定进入队列），营业厅ID：" + dwObjectId);
				getRoomUserNumber();
				break;
			//数据同步完成回调
			case AnyChatObjectDefine.ANYCHAT_OBJECT_EVENT_SYNCDATAFINISH:
				switch (dwObjectType) {
					case AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA://营业厅
						CLog.i(TAG, "服务区域(营业厅)数据同步完成：dwObjectId = " + dwObjectId);
//						mView.loginResult(0);
						break;
					case AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AGENT:
						CLog.i(TAG, "客服对象数据同步完成： dwObjectId = " + dwObjectId);
						break;
					case AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE:
						CLog.i(TAG, "队列对象数据同步完成： dwObjectId = " + dwObjectId);
						getRoomUserNumber();
						break;
					default:
						CLog.i(TAG, "数据同步完成： dwObjectId = " + dwObjectId);
						break;
				}
				break;
			//离开服务区域回调
			case AnyChatObjectDefine.ANYCHAT_AREA_EVENT_LEAVERESULT:
				CLog.i(TAG, "退出营业厅： dwObjectId" + dwObjectId);
				break;
		}
	}

	/**获取队列数据*/
	private void getRoomUserNumber() {
		int[] queueIds = AnyChatCoreSDK.ObjectGetIdList(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE);//获取营业厅的业务队列列表Id数组
		if(queueIds.length > 0) {
			boolean found = false;
			for (int id : queueIds) {
				if (id == Global.ANY_CHAT_QUEUE_ID) {
					found = true;
				}
			}
			queueId = found ? Global.ANY_CHAT_QUEUE_ID : queueIds[0];

			String name = AnyChatCoreSDK.ObjectGetStringValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueId, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_NAME);
			int number = AnyChatCoreSDK.ObjectGetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueId, AnyChatObjectDefine.ANYCHAT_QUEUE_INFO_LENGTH);
			int front = AnyChatCoreSDK.ObjectGetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueId, AnyChatObjectDefine.ANYCHAT_QUEUE_INFO_BEFOREUSERNUM);
			CLog.i(TAG, "队列名称：" + name + "，队列ID：" + queueId + "， 当前人数：" + number + "， 前面的人数：" + front);
			mView.roomUserNum(number, front);
		} else {
			mView.loginResult(-1);
		}
	}
}
